package com.example.project.service.impl;

import com.example.project.common.MyTool;
import com.example.project.dao.ExternalUnitBinDao;
import com.example.project.entity.ExternalUnit;
import com.example.project.dao.ExternalUnitDao;
import com.example.project.entity.ExternalUnitBin;
import com.example.project.entity.SafetyInspe;
import com.example.project.entity.SafetyInspeBin;
import com.example.project.service.ExternalUnitService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (ExternalUnit)表服务实现类
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@Service("externalUnitService")
public class ExternalUnitServiceImpl implements ExternalUnitService {
    @Resource
    private ExternalUnitDao externalUnitDao;
    @Resource
    private ExternalUnitBinDao externalUnitBinDao;

    private MyTool myTool = new MyTool();

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ExternalUnit queryById(Integer id) {
        return this.externalUnitDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param externalUnit 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ExternalUnit> queryByPage(ExternalUnit externalUnit, PageRequest pageRequest, Date startTime, Date endTime) {
        long total = this.externalUnitDao.count(externalUnit);
        return new PageImpl<>(this.externalUnitDao.queryAllByLimit(externalUnit, pageRequest, startTime, endTime), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param externalUnit 实例对象
     * @return 实例对象
     */
    @Override
    public ExternalUnit insert(ExternalUnit externalUnit) {
        //创建时间
        externalUnit.setCreatTime(myTool.dateNow());
        this.externalUnitDao.insert(externalUnit);
        return externalUnit;
    }

    /**
     * 修改数据
     *
     * @param externalUnit 实例对象
     * @return 实例对象
     */
    @Override
    public ExternalUnit update(ExternalUnit externalUnit) {
        this.externalUnitDao.update(externalUnit);
        return this.queryById(externalUnit.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.externalUnitDao.deleteById(id) > 0;
    }
    /**
     * 删除进入回收站
     * */
    @Override
    public Boolean deleteById1(Integer id) {
        //获取当前id的数据
        ExternalUnit externalUnit = this.externalUnitDao.queryById(id);
        externalUnit.setRecycle(2);
        //创建回收站数据
        ExternalUnitBin externalUnitBin = new ExternalUnitBin();
        externalUnitBin.setExternalUnitId(externalUnit.getId());
        externalUnitBin.setBinTime("0,1");
        //生成当前删除时的时间
        externalUnitBin.setDeleteTime(myTool.dateNow());
        //绑定
        this.externalUnitBinDao.insert(externalUnitBin);

        return this.externalUnitDao.update(externalUnit)>0;
    }
}
